<?php

namespace App\Repository\Repositories;

use App\Repository\Models\Products;
use App\Repository\Models\Shop;
use App\Repository\Repositories\Interfaces\ShopRepository;

/**
 * Class ShopRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ShopRepositoryEloquent extends BaseRepository implements ShopRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'id',
        'sp_uid',
        'sp_status',
        'sp_name'=>'like',
        'sp_created' => 'between',
        'updated_at' => 'between',
        'sp_id'=>'in'
    ];

    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'company',
        'companyMaterial',
        'shopLevel',
        'product',
        'user',
        'shopBanners',
    ];
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Shop::class;
    }

    public function comment($comment){
        $shop = $this->find();
    }

    /**
     * 商城首页推荐店铺
     * @return array
     */
    public function recommendshop(){
        $shop=Shop::query()->with(['companyMaterial'])->where('is_recommend',1)->where('sp_status',1)->limit(9)->get(['sp_id','sp_name','product_comment_score','product_comment_count','seller_comment_score','seller_comment_count','logistics_comment_score','logistics_comment_count','description','sp_cmp_id'])->toArray();

        foreach($shop as $k=>$v){
            $product=Products::query()->where('is_for_sale',1)->where('status',1)->where('is_recommend',1)->where('product_type',1)->where('shop_id',$v['sp_id'])->with(['skus'=>function($skus){
                    $skus->limit(1);
                }])->limit(3)->get(['id','cover_image','shop_id'])->toArray();
            $shop[$k]['product']=$product;
        }
        return $shop;
    }
    /**
     * 商城首页推荐店铺 / 仅 按店铺 is_recommend 查询
     * @return array
     */
    public function recomShop(){
        $shop=Shop::query()->with(['companyMaterial'])->where('is_recommend',1)->where('sp_status',1)->limit(8)->get(['sp_id','sp_name','product_comment_score','product_comment_count','seller_comment_score','seller_comment_count','logistics_comment_score','logistics_comment_count','description','sp_cmp_id'])->toArray();

        foreach($shop as $k=>$v){
            $product=Products::query()->where('is_for_sale',1)->where('status',1)->where('is_index_recommend',1)->where('product_type',1)->where('shop_id',$v['sp_id'])->with(['skus'=>function($skus){
                    $skus->limit(1);
                }])->limit(3)->get(['id','cover_image','shop_id'])->toArray();
            $shop[$k]['product']=$product;
        }
        return $shop;
    }

}
